package com.pagoda.nerp.web.services;

import com.pagoda.nerp.web.constant.ErrorEnum;
import com.pagoda.nerp.web.ehr.*;
import com.pagoda.nerp.web.entities.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.ws.client.core.support.WebServiceGatewaySupport;
import org.springframework.ws.soap.client.core.SoapActionCallback;

import java.util.ArrayList;
import java.util.List;

import static com.pagoda.nerp.web.util.JaxbObjectAndXmlUnit.xml2Object;
/**
 *  @Description:  通过WebService 获取EHR接口的数据，实现XML与实体的转化
 *  @Date: 17:26 2017/12/28
 */
@Service
public class EHRService extends WebServiceGatewaySupport {
    @Value("${client.url}")
    private String clienturl;
    @Value("${erpclient.url}")
    private String erpclienturl;
    @Value("${callback.hrunit.url}")
    private String hrunitcburl;
    @Value("${callback.hrpost.url}")
    private String hrpostcburl;
    @Value("${callback.hrzgzd.url}")
    private String hrzgzdcburl;
    @Value("${callback.hrzgzderp.url}")
    private String hrzgzderpcburl;

    //获取HRUnit数据
    public String getHROrgXMLStr(String date) {
        GetHROrgDataForYTH request = new GetHROrgDataForYTH();
        request.setDate(date);

        //发送请求查询指定时间的HR数据库的数据
        GetHROrgDataForYTHResponse response = (GetHROrgDataForYTHResponse) getWebServiceTemplate()
                .marshalSendAndReceive(clienturl,
                        request,
                        new SoapActionCallback(hrunitcburl));
        String xmlstr = response.getGetHROrgDataForYTHResult();
        return xmlstr.toLowerCase();
    }

    //获取HRZGZD数据
    public String getHRZGZDXMLStr(String date){
        GetHRZGZDDataForYTH request = new GetHRZGZDDataForYTH();
        request.setDate(date);

        GetHRZGZDDataForYTHResponse response = (GetHRZGZDDataForYTHResponse)getWebServiceTemplate()
                .marshalSendAndReceive(clienturl,
                        request,
                        new SoapActionCallback(hrzgzdcburl));
        String xmlstr = response.getGetHRZGZDDataForYTHResult();
        return xmlstr.toLowerCase();
    }

    //获取HROrgPost数据
    public String getHROrgPostXMLStr(String date){
        GetHROrgPostForYTH request = new GetHROrgPostForYTH();
        request.setDate(date);

        GetHROrgPostForYTHResponse response = (GetHROrgPostForYTHResponse)getWebServiceTemplate()
                .marshalSendAndReceive(clienturl,
                        request,
                        new SoapActionCallback(hrpostcburl));
        String xmlstr = response.getGetHROrgPostForYTHResult();
        return xmlstr.toLowerCase();
    }

    //获取ERP映射数据
    public String getHRZGZDERPXMLStr(String date){
        GetHRZGZDDataForERP request = new GetHRZGZDDataForERP();
        request.setDate(date);

        GetHRZGZDDataForERPResponse response = (GetHRZGZDDataForERPResponse)getWebServiceTemplate()
                .marshalSendAndReceive(erpclienturl,
                        request,
                        new SoapActionCallback(hrzgzderpcburl));
        String xmlstr = response.getGetHRZGZDDataForERPResult();
        return xmlstr.toLowerCase();
    }

    public List<HRUnit> getHRORGINFOList(String date)
    {
        String xmlstr = getHROrgXMLStr(date);   //获取HRUnit数据
        return ((HRORGINFOList)xml2Object(xmlstr,HRORGINFOList.class)).getHRORGINFOList();
    }

    public List<HRZGZD> getHRZGZDList(String date)
    {
        List<HRZGZD> list = new ArrayList<>();
        String xmlstr = getHRZGZDXMLStr(date);
        if(xmlstr==null || xmlstr.contains(ErrorEnum.ERROR_XML_NODE.getErrorString())){
            return list;
        }
        try {
            list = ((HRZGZDList) xml2Object(xmlstr, HRZGZDList.class)).getHRZGZDList();
        }catch (Exception e){
            return list;
        }
        return list;
    }

    public List<HRORGPost> getHRORGPostList(String date){
        String xmlstr = getHROrgPostXMLStr(date);
        return ((HRORGPOSTList)xml2Object(xmlstr,HRORGPOSTList.class)).getHRORGPostList();
    }
}